<template>
  <div class="attraction-detail">
    <!-- 头部图片轮播 -->
    <div class="hero-section">
      <el-carousel height="400px">
        <el-carousel-item v-for="(image, index) in attraction.imageUrls" :key="index">
          <el-image :src="image" fit="contain" :alt="attraction.name" class="hero-image" />
        </el-carousel-item>
      </el-carousel>

      <!-- 浮动操作按钮 -->
      <div class="floating-actions">
        <el-button circle :icon="Share" @click="handleShare" />
        <el-button circle :icon="Star" :type="isFavorited ? 'primary' : 'default'" @click="toggleFavorite" />
      </div>
    </div>

    <div class="content-wrapper">
      <!-- 基本信息 -->
      <div class="basic-info">
        <h1 class="attraction-title">{{ attraction.name }}</h1>
        <div class="rating-location">
          <div class="rating">
            <el-rate v-model="attraction.rating" disabled show-score />
          </div>
          <div class="location">
            <el-icon><Location /></el-icon>
            <span>{{ attraction.address }}</span>
          </div>
        </div>

        <!-- 标签 -->
        <div class="tags" v-if="attraction.tagsJson">
          <el-tag v-for="tag in attraction.tagsJson" :key="tag" class="tag">{{ tag }}</el-tag>
        </div>
      </div>

      <!-- 快速信息卡片 -->
      <el-row :gutter="16" class="quick-info">
        <el-col :span="8">
          <el-card class="info-card">
            <div class="info-item">
              <el-icon class="info-icon"><Clock /></el-icon>
              <div>
                <div class="info-label">开放时间</div>
                <div class="info-value">{{ attraction.openHours }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="info-card">
            <div class="info-item">
              <el-icon class="info-icon"><Tickets /></el-icon>
              <div>
                <div class="info-label">门票价格</div>
                <div class="info-value">
                  <span v-if="false">¥{{ attraction.price }}</span>
                  <span v-else>免费</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="info-card">
            <div class="info-item">
              <el-icon class="info-icon"><Timer /></el-icon>
              <div>
                <div class="info-label">日均人流量</div>
                <div class="info-value">{{ attraction.visitors }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 详细介绍 -->
      <el-card class="section-card">
        <template #header>
          <h2>景点介绍</h2>
        </template>
        <div class="description">
          <p>{{ attraction.description }}</p>
        </div>
      </el-card>

      <!-- 图片画廊 -->
      <el-card class="section-card" v-if="false">
        <template #header>
          <h2>精彩瞬间</h2>
        </template>
        <div class="gallery">
          <div v-for="(image, index) in gallery" :key="index" class="gallery-item" @click="previewImage(index)">
            <img :src="image" :alt="`景点图片${index + 1}`" />
          </div>
        </div>
      </el-card>

      <!-- 实用信息 -->
      <el-card class="section-card" v-if="false">
        <template #header>
          <h2>实用信息</h2>
        </template>
        <div class="practical-info">
          <div class="info-row">
            <span class="label">交通方式：</span>
            <span>{{ attraction.transportation }}</span>
          </div>
          <div class="info-row">
            <span class="label">最佳季节：</span>
            <span>{{ attraction.bestSeason }}</span>
          </div>
          <div class="info-row">
            <span class="label">联系电话：</span>
            <span>{{ attraction.phone }}</span>
          </div>
        </div>
      </el-card>

      <!-- 用户评价 -->
      <el-card class="section-card" v-if="false">
        <template #header>
          <div class="reviews-header">
            <h2>用户评价</h2>
            <el-button type="primary" @click="showReviewDialog = true">写评价</el-button>
          </div>
        </template>
        <div class="reviews">
          <div v-for="review in reviews" :key="review.id" class="review-item">
            <div class="review-header">
              <el-avatar :src="review.avatar" :alt="review.username" />
              <div class="review-info">
                <div class="username">{{ review.username }}</div>
                <div class="review-meta">
                  <el-rate v-model="review.rating" disabled size="small" />
                  <span class="review-date">{{ review.date }}</span>
                </div>
              </div>
            </div>
            <div class="review-content">{{ review.content }}</div>
            <div v-if="review.images" class="review-images">
              <img v-for="(img, idx) in review.images" :key="idx" :src="img" :alt="`评价图片${idx + 1}`" @click="previewReviewImage(img)" />
            </div>
          </div>
        </div>
      </el-card>

      <!-- 相关推荐 -->
      <el-card class="section-card" v-if="false">
        <template #header>
          <h2>相关推荐</h2>
        </template>
        <el-row :gutter="16">
          <el-col :span="8" v-for="item in relatedAttractions" :key="item.id">
            <div class="related-item" @click="goToAttraction(item.id)">
              <img :src="item.image" :alt="item.name" />
              <div class="related-info">
                <h4>{{ item.name }}</h4>
                <div class="related-rating">
                  <el-rate v-model="item.rating" disabled size="small" />
                  <span>{{ item.rating }}</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>

    <!-- 评价对话框 -->
    <el-dialog v-model="showReviewDialog" title="写评价" width="500px">
      <el-form :model="newReview" label-width="80px">
        <el-form-item label="评分">
          <el-rate v-model="newReview.rating" />
        </el-form-item>
        <el-form-item label="评价内容">
          <el-input v-model="newReview.content" type="textarea" :rows="4" placeholder="分享你的游玩体验..." />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showReviewDialog = false">取消</el-button>
        <el-button type="primary" @click="submitReview">提交评价</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Share, Star, Location, Clock, Tickets, Timer } from '@element-plus/icons-vue'
import { ElMessage, ElImageViewer } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import http from '@/apis/scene'

const router = useRouter()
const route = useRoute()

// 响应式数据
const isFavorited = ref(false)
const showReviewDialog = ref(false)
const gallery = ref([])
const reviews = ref<Array<any>>([])

// 景点数据
const attraction = ref({
  name: '',
  province: '云南省',
  city: '昆明市',
  address: '东川区',
  description: '',
  category: '',
  openHours: '08:30-17:00',
  price: 0,
  rating: 4.8,
  tagsJson: `[]`,
  visitors: 8000,
  transportation: '',
  bestSeason: '',
  phone: '',
  imageUrls: []
})

// 相关推荐数据
const relatedAttractions = ref([
  {
    id: 2,
    name: '黄龙风景名胜区',
    image: '/placeholder.svg?height=150&width=200',
    rating: 4.6
  },
  {
    id: 3,
    name: '峨眉山',
    image: '/placeholder.svg?height=150&width=200',
    rating: 4.5
  },
  {
    id: 4,
    name: '都江堰',
    image: '/placeholder.svg?height=150&width=200',
    rating: 4.4
  }
])

// 新评价数据
const newReview = reactive({
  rating: 5,
  content: ''
})

// 方法
const toggleFavorite = () => {
  isFavorited.value = !isFavorited.value
  ElMessage.success(isFavorited.value ? '已添加到收藏' : '已取消收藏')
}

const handleShare = () => {
  ElMessage.success('分享功能开发中...')
}

const previewImage = (index: number) => {
  // ElImageViewer({
  //   urlList: attraction.gallery,
  //   initialIndex: index
  // })
}

const previewReviewImage = (imageUr: string) => {
  // ElImageViewer({
  //   urlList: [imageUrl]
  // })
}

const submitReview = () => {
  if (!newReview.content.trim()) {
    ElMessage.warning('请填写评价内容')
    return
  }

  ElMessage.success('评价提交成功！')
  showReviewDialog.value = false
  newReview.content = ''
  newReview.rating = 5
}

const goToAttraction = (id: number) => {
  ElMessage.info(`跳转到景点 ${id}`)
}
// 生命周期
onMounted(() => {
  if (route.query.id) {
    http.fetchById(route.query.id as string).then((response) => {
      let { code, message, result } = response.data
      if (code !== 200) {
        ElMessage.error(message)
        return
      }
      attraction.value = result
      attraction.value.imageUrls = JSON.parse(result.imageUrls)
      attraction.value.tagsJson = JSON.parse(result.tagsJson)
      // console.log(attraction.value.images)
    })
  }
  // http.fetchById()
})
</script>

<style scoped>
.attraction-detail {
  max-width: var(--page-max-width);
  margin: 0 auto;
  background: #f5f7fa;
  padding-bottom: 10px;
}

.hero-section {
  position: relative;
  margin-bottom: 20px;
}

.hero-image {
  width: 100%;
  height: 400px;
  object-fit: cover;
}

.floating-actions {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  gap: 10px;
}

.content-wrapper {
  padding: 0 10px;
}

.basic-info {
  background: white;
  padding: 10px;
  border-radius: 12px;
  margin-bottom: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.attraction-title {
  font-size: 28px;
  font-weight: bold;
  color: #2c3e50;
  margin: 0 0 16px 0;
}

.rating-location {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.rating {
  display: flex;
  align-items: center;
  gap: 8px;
}

.review-count {
  color: #666;
  font-size: 14px;
}

.location {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #666;
}

.tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tag {
  border-radius: 16px;
}

.quick-info {
  margin-bottom: 10px;
}

.info-card {
  text-align: center;
}

.info-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.info-icon {
  font-size: 24px;
  color: #409eff;
}

.info-label {
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
}

.info-value {
  font-size: 16px;
  font-weight: bold;
  color: #2c3e50;
}

.section-card {
  margin-bottom: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.section-card :deep(.el-card__header) {
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.description p {
  line-height: 1.8;
  margin-bottom: 16px;
  color: #555;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.gallery-item {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s;
}

.gallery-item:hover {
  transform: scale(1.05);
}

.gallery-item img {
  width: 100%;
  height: 150px;
  object-fit: cover;
}

.practical-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-row {
  display: flex;
  align-items: center;
}

.info-row .label {
  font-weight: bold;
  color: #2c3e50;
  min-width: 100px;
}

.reviews-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.reviews {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.review-item {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.review-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.review-info {
  flex: 1;
}

.username {
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 4px;
}

.review-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.review-date {
  color: #999;
  font-size: 12px;
}

.review-content {
  line-height: 1.6;
  color: #555;
  margin-bottom: 12px;
}

.review-images {
  display: flex;
  gap: 8px;
}

.review-images img {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
}

.related-item {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s;
  background: white;
}

.related-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.related-item img {
  width: 100%;
  height: 120px;
  object-fit: cover;
}

.related-info {
  padding: 12px;
}

.related-info h4 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.related-rating {
  display: flex;
  align-items: center;
  gap: 4px;
}

@media (max-width: 768px) {
  .content-wrapper {
    padding: 0 10px;
  }

  .rating-location {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .gallery {
    grid-template-columns: repeat(2, 1fr);
  }

  .quick-info .el-col {
    margin-bottom: 16px;
  }
}
</style>
